<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" title="申请" width="50%" append-to-body>
      <el-form ref="form" :model="formData" class="dialog-form-two">
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库" prop="warehouseId">
              <warehouse-select v-model="formData.warehouseId"></warehouse-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplier">
              <warehouse-select v-model="formData.supplier"></warehouse-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formData.remark" placeholder="请输入" />
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
          >新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="tableData.loading" :data="tableData.list">

        <el-table-column label="名称" prop="name">
          <template #default="scope">
            <el-input placeholder="请输入" v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="名称" prop="name">
          <template #default="scope">
            <category-select v-model="scope.row.categoryId" placeholder="请输入"></category-select>
          </template>
        </el-table-column>

        <el-table-column label="品牌" prop="brand">
          <template #default="scope">
            <el-input placeholder="请输入" v-model="scope.row.brand"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="型号" prop="model">
          <template #default="scope">
            <el-input placeholder="请输入" v-model="scope.row.model"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template  #default="scope">
            <el-link
                       size="primary"
                       @click="handleDelete(scope)"
            >删除</el-link>

          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="sub">提交</el-button>
          <el-button @click="close">取 消</el-button>
        </el-row>

      </div>
    </el-dialog>



  </div>
</template>

<script setup>

import api from "./api.js";
import CategorySelect from "@/components/Asset/CategorySelect.vue";
import WarehouseSelect from "@/components/Consume/WarehouseSelect.vue";

const emptyItem = {
  number: null,
  num: 1,
}

const form = ref();

let isEdit = false;

const openFlag = ref(false);

// 初始化的数据
const formDataInit = {
  id: null,
  warehouseId: null,
  supplier: null,
  remark: null
}

let deletedItem = [];

const formData = reactive(formDataInit);

const tableData = reactive({
  loading: false,
  list: []
})

function reset() {
  Object.assign(formData, formDataInit);
  tableData.list = [];
}

function open(row) {
  openFlag.value = true;
  reset();
  if (row) {
    isEdit = true;
    api.detail(row).then(response=> {
      Object.assign(this.formData, response);
      tableData.list = response.relItems;
    })
  } else {
    isEdit = false;
  }
}

function close() {
  openFlag.value = false;
}

function handleAdd() {
  tableData.list.push({...emptyItem});
}

function handleDelete({ row, $index }) {
  // 如果是编辑状态需要把删除的行存起来
  if (isEdit) {
    deletedItem.push(row);
  }
  tableData.list.splice($index, 1);
}

function getParams() {
  return {
    edit: isEdit,
    apply: { ...formData },
    items: tableData.list,
    itemsDel: deletedItem
  }
}

/** 提交按钮 */
function save() {
  const params = getParams();
  api.save(params).then(response => {
    this.$emit("sub");
    this.close();
  });
}

function sub() {
  const params = getParams();
  api.saveAndSub(params).then(response => {
    this.$emit("sub");
    this.close();
  });
}


</script>

